# 导入pymysql模块
import json

import pymysql
from flask import Flask, request

app = Flask(__name__)

# 建立与数据库的连接
conn = pymysql.connect(
    host="192.168.1.129",
    port=3306,
    user="admin1",
    password="123",
    database="mumushouji",
    charset="utf8",
    cursorclass=pymysql.cursors.DictCursor,
    autocommit=True
)
# 实例化一个游标对象
cursor = conn.cursor()

@app.route("/", methods=["post"])
def update_user_info():
    request_data = request.data
    print(request_data)
    # 转成json，也就是dict
    request_data = json.loads(request_data)
    print(request_data)
    username = request_data["username"]
    nickname = request_data["nickname"]
    print(username)
    print(nickname)
    sql = "update user set nickname=%(nickname)s where username=%(username)s"
    params = {
        "username":username,
        "nickname":nickname
    }
    r = cursor.execute(sql,params)
    # 数据提交，手动提交方式
    # conn.commit()
    # r返回的是影响的数据条数
    print(r)
    return "修改用户信息成功"

if __name__ == "__main__":
    app.run()

